<?php

namespace App\Hospital\Controllers;

use App\Hospital\Models\FamilyModel;
use App\Hospital\Repositories\HealthPerson;
use App\Models\Area;
use App\Models\HospitalTeam;
use App\Models\HospitalUserFamily;
use App\Models\ImportFamily;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Facades\DB;

class HealthPersonController extends AdminController
{
    protected function description()
    {
        $this->description['index'] = '签约居民超龄（孕龄）后将转为健康人群';

        return $this->description;
    }
    protected function title()
    {
        return '健康人群';
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $hospital_id = Admin::user()->hospital_id;
        return Grid::make(new \App\Hospital\Repositories\HospitalUserFamily(['user','family','team','import']), function (Grid $grid) use($hospital_id) {
            $hospitalUserFamily = HospitalUserFamily::query()
                ->select('family_id',DB::raw('MAX(id) as id'))
                ->where('hospital_id',$hospital_id)
                ->groupBy('family_id');

            $grid->model()->joinSub($hospitalUserFamily, 'latest_huf', function (JoinClause $join) {
                $join->on('eb_hospital_user_family.id', '=', 'latest_huf.id');
            })->where('hospital_id',$hospital_id);

            $grid->column('family.name','姓名');
            $grid->column('user.phone','联系电话')->display(function($value){
                return substr_replace($value, '****', 3, 4);
            });
            $grid->column('family.idcard_type','证件类型')->using(FamilyModel::$idCardTypeTxt);
            $grid->column('family.idcard','证件号')->display(function($value){
                return $value?substr_replace($value, '********', 6, 8):'';
            });
            $grid->column('family.birthday','生日');

            $grid->column('family.address','现住址')->display(function($value){

                $area = Area::query()->whereIn('id',[$this->province,$this->city,$this->county])->pluck('title');
                return implode(',',$area->toArray()).$value;
            });
            $grid->column('team.name','签约团队名称');
            $grid->column('status')->using(HospitalUserFamily::$statusTxt);
            $grid->column('type')->using(HospitalUserFamily::$typeTxt);
            $grid->column('starttime','本次签约时间')->display(function($value){

                return $this->starttime."至".$this->endtime;
            });
            $grid->column('created_at','首次签约日期')->display(function($value){

                return date('Y-m-d',strtotime($this->created_at));
            });
            Tooltip::make('.icon-help-circle')->title('此标记为该用户是否与同步数据关联成功');

            $grid->column('import','是否管辖<i class="feather icon-help-circle"></i>')->display(function ($value){
                return $value->pluck('type');
            })->map(function($value){

                return ImportFamily::$typeTxt[$value];
            })->badge();



            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->equal('type')->select(HospitalUserFamily::$typeTxt);
                $filter->equal('status')->select(HospitalUserFamily::$statusTxt);
                $filter->equal('import.type','管辖类型')->select(ImportFamily::$typeTxt);
                $filter->like('family.name','签约人姓名');
                $filter->equal('user.phone','联系电话');
                $filter->between('created_at','首次签约时间')->date();
                $filter->between('starttime','本次签约时间')->date();
                $filter->equal('team.id','签约团队')->select(HospitalTeam::query()->where('hospital_id',Admin::user()->hospital_id)->pluck('name','id')->toArray());
            });




            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableQuickEditButton();
            $grid->disableRowSelector();
            $grid->showColumnSelector();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HealthPerson(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('family_id');
            $show->field('hospital_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HealthPerson(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('family_id');
            $form->text('hospital_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
